#!/usr/bin/env python
# -*- coding: UTF-8 -*-

#
# CLAtividades - Atividades Pedagógicas
# Copyright (C) 2007 Marcelo Lira dos Santos <setanta@gmail.com>
#
# CLAtividades is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# CLAtividades is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#

import os
from os.path import join
import re
from distutils.sysconfig import get_config_var
from distutils.sysconfig import get_python_version
from distutils.core import setup
from distutils.command.install import install
import glob

class my_install(install):
    def run(self):
        preprocess(join('src', 'clatividades.py.in'))
        install.run(self)

def preprocess(path):
    if os.path.exists(path):
        src = open(path, "r")
        target = open(path.strip(".in"), "w")

        lines = src.readlines()[:]

        for line in lines:
            line = re.sub("@PREFIX@", get_config_var('prefix'), line)
            line = re.sub("@PYTHONVERSION@", get_python_version(), line)
            target.write(line)

        target.close()
        src.close()


# read the version from the clatividades ChangeLog
clatividades_version = os.popen('cat ChangeLog | grep -m 1 ^clatividades | ' \
                                'cut -d" " -f2').read().strip()

# files to install
data_dir      = 'data'
icons_dir     = join(data_dir, 'icons')

inst_share    = [join(data_dir, 'clatividades.glade'),
                 join(data_dir, 'cla_header.png'),
                 join(data_dir, 'clatividades.db')
]
inst_desktop  = [join(data_dir, 'clatividades.desktop')]
inst_icons    = [join(icons_dir, 'all.png'),
                 join(icons_dir, 'biol.png'),
                 join(icons_dir, 'board.png'),
                 join(icons_dir, 'chem.png'),
                 join(icons_dir, 'math.png'),
                 join(icons_dir, 'phys.png')
]

data_files = [
    (join('share', 'clatividades'), inst_share),
    (join('share', 'applications'), inst_desktop),
    (join('share', 'pixmaps'),      inst_icons),
]

setup(
    name             = 'clatividades',
    version          = clatividades_version,
    description      = 'Atividades pedagógicas do Ciência Livre',
    long_description = 'Interface de acesso às atividades pedagógicas do Ciência Livre',
    author           = 'Marcelo Lira dos Santos',
    author_email     = 'setanta@gmail.com',
    url              = 'http://code.google.com/p/clatividades',
    license          = 'GPL',
    scripts          = ['src/clatividades'],
    packages         = ['clatividades'],
    package_dir      = {'clatividades' : 'src'},
    cmdclass         = { 'install' : my_install },
    data_files       = data_files
)

